package net.stogi.dsper.ui.client.module.datasource;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.gxt.client.core.model.Model;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.common.mvp.core.model.KeyValues;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.google.gwt.user.client.ui.Widget;

class DataSourceSelectorWidget extends Widget implements DataSourceSelectorView {

	private final Button cancelButton = new Button();

	private final Grid<ModelData> dataSourceGrid;

	private final ListStore<ModelData> dataSourceStore = new ListStore<ModelData>();

	private final Window mainWindow = new Window();

	private final Button selectButton = new Button();

	public DataSourceSelectorWidget() {
		List<ColumnConfig> columnConfigList = new ArrayList<ColumnConfig>();

		ColumnConfig columnConfig = new ColumnConfig();
		columnConfig.setHeader("Name");
		columnConfig.setId("name");
		columnConfig.setWidth(400);
		columnConfigList.add(columnConfig);

		dataSourceGrid = new Grid<ModelData>(dataSourceStore, new ColumnModel(columnConfigList));

		initComposite();
		initLayout();
	}

	@Override
	public void appear() {
		mainWindow.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainWindow.hide();
		doClearDataSourceList();
	}

	@Override
	public void displayDataSources(List<KeyValues<Long>> dataSourceList) {
		doClearDataSourceList();
		List<ModelData> rowList = new ArrayList<ModelData>();
		for (KeyValues<Long> dataSource : dataSourceList) {
			BeanModel row = new Model();
			row.set("__bean", dataSource);
			for (String param : dataSource.getParams()) {
				row.set(param, dataSource.getValue(param));
			}
			rowList.add(row);
		}
		dataSourceStore.add(rowList);
	}

	@Override
	public void setCancelBehavior(final Behavior behavior) {
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setSelectBehavior(final BehaviorWithValue<List<KeyValues<Long>>> behavior) {
		selectButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				List<KeyValues<Long>> values = new ArrayList<KeyValues<Long>>();
				for (ModelData selectedItem : dataSourceGrid.getSelectionModel().getSelectedItems()) {
					values.add(selectedItem.<KeyValues<Long>> get("__bean"));
				}
				behavior.behave(values);
			}
		});
	}

	private void doClearDataSourceList() {
		dataSourceStore.removeAll();
	}

	private void initComposite() {
		selectButton.setText("Select");
		cancelButton.setText("Cancel");

		dataSourceGrid.getSelectionModel().setSelectionMode(SelectionMode.MULTI);
		dataSourceGrid.setAutoExpandColumn("name");
		dataSourceGrid.setAutoHeight(true);
		dataSourceGrid.setBorders(true);
		dataSourceGrid.setHeight(100);
		dataSourceGrid.setMinColumnWidth(200);

		mainWindow.setClosable(false);
		mainWindow.setHeading("Data Source Selector");
		mainWindow.setFrame(true);
		mainWindow.setModal(true);
		mainWindow.setResizable(false);
		mainWindow.setWidth("30%");
		mainWindow.setAutoHeight(true);
		mainWindow.setVisible(false);
	}

	private void initLayout() {
		mainWindow.add(dataSourceGrid);

		mainWindow.addButton(cancelButton);
		mainWindow.addButton(selectButton);
	}
}
